package com.leadingsoft.web.code.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leadingsoft.common.constant.Constant;
import com.leadingsoft.core.dto.BaseQuery;
import com.leadingsoft.core.dto.Pageable;
import com.leadingsoft.core.dto.ResultType;
import com.leadingsoft.core.mapper.CodeCategoryMapper;
import com.leadingsoft.core.mapper.CodeMapper;
import com.leadingsoft.core.mapper.CodeTypeMapper;
import com.leadingsoft.core.mapper.UserInfoMapper;
import com.leadingsoft.core.pojo.Code;
import com.leadingsoft.core.pojo.CodeCategory;
import com.leadingsoft.core.pojo.CodeCategoryExample;
import com.leadingsoft.core.pojo.CodeExample;
import com.leadingsoft.core.pojo.CodeType;
import com.leadingsoft.core.pojo.CodeTypeExample;
import com.leadingsoft.core.pojo.CodeTypeExample.Criteria;
import com.leadingsoft.core.pojo.UserInfo;
import com.leadingsoft.web.code.dto.CodeDto;
import com.leadingsoft.web.code.dto.ViewCodeDto;
import com.leadingsoft.web.code.utils.CommonUtils;

@Service
public class CodeService {
	@Autowired
	private CodeTypeMapper codeTypeMapper;
	@Autowired
	private CodeCategoryMapper codeCategoryMapper;
	@Autowired
	private CodeMapper codeMapper;
	@Autowired
	private UserInfoMapper userInfoMapper;

	public String getCodeTypes() {
		CodeTypeExample codeTypeExample = new CodeTypeExample();
		Criteria criteria = codeTypeExample.createCriteria();
		criteria.andDelFlagEqualTo(Constant.IS_EXIST);
		List<CodeType> codeTypes = codeTypeMapper.selectByExample(codeTypeExample);
		return ResultType.success(codeTypes);
	}
	public String getCodeCategorys() {
		CodeCategoryExample codeCategoryExample = new CodeCategoryExample();
		com.leadingsoft.core.pojo.CodeCategoryExample.Criteria criteria = codeCategoryExample.createCriteria();
		criteria.andDelFlagEqualTo(Constant.IS_EXIST);
		List<CodeCategory> codeCategories = codeCategoryMapper.selectByExample(codeCategoryExample);
		return ResultType.success(codeCategories);
	}
	public String getList(BaseQuery baseQuery, Integer uId, String type, String category, int isCreate) {
		int page = baseQuery.getPage();
		int size = baseQuery.getSize();
		CodeExample codeExample = new CodeExample();
		com.leadingsoft.core.pojo.CodeExample.Criteria criteria = codeExample.createCriteria();
		criteria.andDelFlagEqualTo(Constant.IS_EXIST);
		criteria.andCodePrivateEqualTo(Constant.IS_PUBLIC);
		if (StringUtils.isNotBlank(type))// 是否指定代码类型
			criteria.andCodeTypeEqualTo(type);
		if (StringUtils.isNotEmpty(category))// 是否指定类别
			criteria.andCodeCategoryEqualTo(category);
		if (null != uId)// 是否指定用户
			criteria.andAddUserEqualTo(uId);
		if (isCreate == 1) {// 按创建时间倒叙
			codeExample.setOrderByClause("add_time desc");
		} else if (isCreate == 2) {// 按照更新时间倒叙
			codeExample.setOrderByClause("edit_time desc");
		}
		PageHelper.startPage(page + 1, size);
		List<Code> codes = codeMapper.selectByExample(codeExample);
		List<CodeDto> codeDtos = new ArrayList<CodeDto>();
		long nowTime = new Date().getTime();
		if (CollectionUtils.isNotEmpty(codes)) {
			codes.forEach((code) -> {
				CodeDto codeDto = new CodeDto();
				BeanUtils.copyProperties(code, codeDto);
				UserInfo userInfo = userInfoMapper.selectByPrimaryKey(code.getAddUser());
				codeDto.setCodeAuthor(userInfo.getName());
				long editTime = code.getEditTime().getTime();
				String lastEditTime = CommonUtils.getLastEditTime(nowTime - editTime);
				codeDto.setLastEdit(lastEditTime);
				codeDtos.add(codeDto);
			});
			PageInfo<Code> pageInfo = new PageInfo<Code>(codes);
			Pageable pageable = new Pageable(page, size, (int) pageInfo.getTotal());
			return ResultType.resultList("success", codeDtos, pageable);
		} else {
			Pageable pageable = new Pageable();
			pageable.setSize(10);
			return ResultType.resultList("success", null, pageable);
		}
	}
	public String saveCode(Integer uId, Code code) {
		Date now = new Date();
		code.setAddUser(uId);
		code.setAddTime(now);
		code.setEditUser(uId);
		code.setEditTime(now);
		code.setDelFlag(Constant.IS_EXIST);
		codeMapper.insertSelective(code);
		return ResultType.success();
	}
	public String viewCode(Integer codeId) {
		Code code = codeMapper.selectByPrimaryKey(codeId);
		ViewCodeDto viewCodeDto = new ViewCodeDto();
		BeanUtils.copyProperties(code, viewCodeDto);
		return ResultType.success(viewCodeDto);
	}
}
